package powerdev.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import powerdev.excepcion.DAOExcepcion;
import powerdev.negocio.GestionPersona;

/**
 * Servlet implementation class RegistrarPersona
 */
@WebServlet("/RegistrarPersona")
public class RegistrarPersona extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarPersona() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
					
		try{
		
			GestionPersona negCan = new GestionPersona();
					
			Integer a=Integer.parseInt(request.getParameter("cboTipPersona"));
			String b=request.getParameter("txtNombre");
			String c=request.getParameter("txtApePaterno");
			String d=request.getParameter("txtApeMaterno");
			Integer e=Integer.parseInt(request.getParameter("cboSexo"));
			Integer f=Integer.parseInt(request.getParameter("cboTipDni"));
			
			String TipoDNI;
			if(f==1){
				TipoDNI = "DNI";
			}else if(f==2){
				TipoDNI = "PASAPORTE";
			}else{
				TipoDNI = "CARNET DE EXTRANJERIA";
			}
			String g=request.getParameter("txtTipDni");
			String h=request.getParameter("txtEmail");
			String h1=request.getParameter("txtEmail1");
			String i=request.getParameter("txtPassword");
			String i1=request.getParameter("txtPassword1");
			Date j = Date.valueOf(request.getParameter("txtFecNacimiento"));
			Integer k=Integer.parseInt(request.getParameter("txtNumCelular"));
			
			
				
			try {
				negCan.insertarPersona(b,c,d,e,TipoDNI,g,k,j,h,i,a);
								
				request.setAttribute("MENSAJE", "Se registro correctamente.");
				
			} catch (DAOExcepcion e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				request.setAttribute("MENSAJE", e1.getMessage());
			}
		
			
		} catch (IllegalArgumentException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
			request.setAttribute("MENSAJE", "ERROR, Falta llenar campos o tipo de variable no soportada: -" + e2.getMessage() + "-");
	}
		
			RequestDispatcher rd = request
					.getRequestDispatcher("RegistrarPersona.jsp");
			rd.forward(request, response);
		
		
				
		}
}

		
	



